package com.ruoyi.web.controller.train;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GfPassenger;
import com.ruoyi.system.service.IGfPassengerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 乘车人信息Controller
 * 
 * @author ruoyi
 * @date 2022-06-16
 */
@RestController
@RequestMapping("/system/passenger")
public class GfPassengerController extends BaseController
{
    @Autowired
    private IGfPassengerService gfPassengerService;

    /**
     * 查询乘车人信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:passenger:list')")
    @GetMapping("/list")
    public TableDataInfo list(GfPassenger gfPassenger)
    {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 不是管理员,则根据当前登录用户的id查询. 管理员返回所有的乘车人.
        if (!user.isAdmin()) {
            Long userId = user.getUserId();
            //将当前登录用户的id变为乘客的所属用户的名字
            gfPassenger.setPassengerUserName(String.valueOf(userId));
        }
        List<GfPassenger> list = gfPassengerService.selectGfPassengerList(gfPassenger);
        return getDataTable(list);
    }

    /**
     * 导出乘车人信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:passenger:export')")
    @Log(title = "乘车人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GfPassenger gfPassenger)
    {
        List<GfPassenger> list = gfPassengerService.selectGfPassengerList(gfPassenger);
        ExcelUtil<GfPassenger> util = new ExcelUtil<GfPassenger>(GfPassenger.class);
        util.exportExcel(response, list, "乘车人信息数据");
    }

    /**
     * 获取乘车人信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:passenger:query')")
    @GetMapping(value = "/{passengerId}")
    public AjaxResult getInfo(@PathVariable("passengerId") String passengerId)
    {
        return AjaxResult.success(gfPassengerService.selectGfPassengerByPassengerId(passengerId));
    }

    /**
     * 新增乘车人信息
     */
    @PreAuthorize("@ss.hasPermi('system:passenger:add')")
    @Log(title = "乘车人信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GfPassenger gfPassenger)
    {
        // 当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        gfPassenger.setPassengerUserName(String.valueOf(userId));
        return toAjax(gfPassengerService.insertGfPassenger(gfPassenger));
    }

    /**
     * 修改乘车人信息
     */
    @PreAuthorize("@ss.hasPermi('system:passenger:edit')")
    @Log(title = "乘车人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GfPassenger gfPassenger)
    {
        return toAjax(gfPassengerService.updateGfPassenger(gfPassenger));
    }

    /**
     * 删除乘车人信息
     */
    @PreAuthorize("@ss.hasPermi('system:passenger:remove')")
    @Log(title = "乘车人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{passengerIds}")
    public AjaxResult remove(@PathVariable String[] passengerIds)
    {
        return toAjax(gfPassengerService.deleteGfPassengerByPassengerIds(passengerIds));
    }
}
